/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bo.apps

import com.android.build.api.dsl.ApplicationExtension
import com.bo.apps.utils.bPrintln
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import java.io.File
import java.io.FileInputStream
import java.util.Properties


internal fun Project.getAppProperties(): Properties? {
    val propertyFile = File(projectDir.absoluteFile, "config.properties")
    if (propertyFile.isFile) {
        val properties = Properties()
        properties.load(FileInputStream(propertyFile))
        val buildType = properties["buildType"].toString()
        "AndroidApplicationConventionPlugin:::打包模式::${if (buildType == "release") "正式环境" else "测试环境"}".bPrintln()
        val configFile: File
        when (buildType) {
            "debug" -> {
                configFile = File(projectDir.absoluteFile, "debug.properties")
            }
            "release" -> {
                configFile = File(projectDir.absoluteFile, "release.properties")
                val releaseProperties = Properties()
                releaseProperties.load(FileInputStream(configFile))
            }
            else -> {
                configFile = File(projectDir.absoluteFile, "debug.properties")
            }
        }
        if (configFile.isFile) {
            val debugProperties = Properties()
            debugProperties.load(FileInputStream(configFile))
            return debugProperties
        }
    }
    return null
}

internal fun Project.configureKotlinApp(
) {


    extensions.configure<ApplicationExtension> {
        getAppProperties()?.let {
            defaultConfig.versionCode = it["versionCode"] as Int?
            defaultConfig.versionName = it["versionName"]?.toString()
        }

    }


}



